package com.lixueju.security.box.validate.code.sms;

import com.lixueju.security.box.core.properties.SecurityBoxConstants;
import com.lixueju.security.box.validate.code.ValidateCode;
import com.lixueju.security.box.validate.code.ValidateCodeException;
import com.lixueju.security.box.validate.code.impl.AbstractValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @author lixueju
 * @since 2019/10/4 11:38
 **/
@Component
public class SmsValidateCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {

    /**
     * 短信验证码发送器
     */
    @Autowired
    private SmsValidateCodeSender smsValidateCodeSender;

    @Override
    protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {
        String paramName = SecurityBoxConstants.DEFAULT_PARAMETER_NAME_MOBILE;
        String mobile = null;
        try {
            mobile = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), paramName);
        } catch (MissingServletRequestParameterException e) {
            throw new ValidateCodeException(paramName + "是必须参数");
        }
        smsValidateCodeSender.send(request,mobile, validateCode.getCode());
    }
}
